﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace JcomTesters.Releases
{


    [jDesc("批量重命名文件", args: new[] {
        "加-前缀","-add-pre-xxx",
        "改-前缀","-up-pre-xxx1 -par2-xxx2",
        "改-后缀名","-up-suf-xxx",
        "改-所有文件名为3位顺序号","-all-order-3n",
        "加-名后缀,所有文件的名","-up-mid-end-xxx",
        //"改-替换文件名","\"-replace 'xxx1 - ' 'xxx2'\"",
        "改-替换文件名","-replace-xxx1 \"-par2-xxx2\"",
        "过滤-按扩展名","-filter-suf-xxx",
        "默认-列表显示","-listd",
        "带参数2","-par2-xxx",
        "带参数3","-par3-xxx",
        "带参数4","-par4-xxx",
        "指定目录-默认当前","-dir-",
        //"指定转出目录-默认当前","-out=",
    }, desc: "变量名的xxx为输出串")]
    class RenameCmd : Ab1Tester
    {
        public override void Run(string[] args)
        {
            var dg = new dginfo();
            var dir = "";
            foreach (var item in args)
            {
                if (item.StartsWith("-debug")) { isDebug = true; continue; }
                else if (item.StartsWith("-add-pre-")) { dg.type = 1; dg.item1 = item.Substring("-add-pre-".Length); continue; }
                else if (item.StartsWith("-up-pre-")) { dg.type = 6; dg.item1 = item.Substring("-up-pre-".Length); continue; }
                else if (item.StartsWith("-up-suf-")) { dg.type = 2; dg.item1 = item.Substring("-up-suf-".Length); continue; }
                else if (item.StartsWith("-all-order-3n-")) { dg.type = 3; dg.item1 = item.Substring("-all-order-3n-".Length); continue; }
                else if (item.StartsWith("-up-mid-end-")) { dg.type = 4; dg.item1 = item.Substring("-up-mid-end-".Length); continue; }
                else if (item.StartsWith("-par2-")) { dg.item2 = item.Substring("-par2-".Length); continue; }//参数1
                else if (item.StartsWith("-par3-")) { dg.item3 = item.Substring("-par3-".Length); continue; }//参数2
                else if (item.StartsWith("-par4-")) { dg.item4 = item.Substring("-par4-".Length); continue; }//参数3
                else if (item.StartsWith("-replace-"))
                {
                    dg.type = 5;
                    dg.item1 = item.Substring("-replace-".Length); continue;
                    //var item1 = item.Substring("-replace '".Length);
                    //if (!item1.EndsWith("'")) { Console.WriteLine("-replace需要单引结尾"); return; }
                    //var dp2 = item1.Substring(0, item1.Length - 1).Split(new string[] { "' '" }, StringSplitOptions.None);
                    //if (dp2.Length != 2) { Console.WriteLine("-replace需要2个参数"); return; }
                    //dg.item1 = dp2[0]; dg.item2 = dp2[1]; continue;
                }
                else if (item.StartsWith("-filter-suf-")) { dg.filter1 = item.Substring("-filter-suf-".Length).ToLower(); continue; }
                else if (item.StartsWith("-listd")) { dg.type = 0; continue; }
                //else if (item.StartsWith("-")) { dg.type = 0; continue; }
                else if (item.StartsWith("-dir-")) { dir = item.Substring("-dir-".Length).Trim(); continue; }

            }
            //if (string.IsNullOrEmpty(dir)) { Console.WriteLine("目录为空"); return; }
            var di2r = System.IO.Path.Combine(this.jtool.GetCurWorkDir(), dir);
            //Directory.Exists的坑(有bug), 目录中含有.或..时，会不存在， 但是实际应该是当前或上级目录，用 new DirectoryInfo可以，
            //
            var didx = new System.IO.DirectoryInfo(di2r);
            if (!didx.Exists) { Console.WriteLine("目录不存在 " + di2r); return; }
            var files = didx.GetFiles();
            if (!string.IsNullOrEmpty(dg.filter1)) files = files.Where(t => t.Extension.ToLower() == "." + dg.filter1).ToArray();
            Console.WriteLine("目录: " + didx.FullName);
            if (dg.type == 0)
            {
                foreach (var item in files)
                {
                    Console.WriteLine(item);
                }
                return;
            }
            if (dg.type == 1)
            {
                if (string.IsNullOrEmpty(dg.item1)) { Console.WriteLine("缺少-add-pre-xxx的xxx参数为前缀"); return; }
                foreach (var item in files)
                {
                    var p2pf = Path.Combine(didx.FullName, dg.item1 + item.Name);
                    if (isDebug) Console.WriteLine("replace: " + item.Name + " to " + dg.item1 + item.Name);
                    item.MoveTo(p2pf);
                }
            }
            else if (dg.type == 5)
            {
                if (string.IsNullOrEmpty(dg.item1)) { Console.WriteLine("需要 参数1"); return; }
                if (string.IsNullOrEmpty(dg.item2)) { Console.WriteLine("需要 参数2"); return; }
                foreach (var item in files)
                {
                    var p2pf = Path.Combine(didx.FullName, item.Name.Replace(dg.item1, dg.item2));
                    if (isDebug) Console.WriteLine("replace: " + item.Name + " to " + item.Name.Replace(dg.item1, dg.item2));
                    item.MoveTo(p2pf);
                }
            }
            else if (dg.type == 6)
            {
                if (string.IsNullOrEmpty(dg.item1)) { Console.WriteLine("需要 参数1"); return; }
                if (string.IsNullOrEmpty(dg.item2)) { Console.WriteLine("需要 参数2"); return; }
                foreach (var item in files)
                {
                    if (item.Name.IndexOf(dg.item1) == 0)
                    {
                        var p2pf = Path.Combine(didx.FullName, dg.item2 + item.Name.Substring(dg.item1.Length));
                        item.MoveTo(p2pf);
                    }
                }
            }
            else
            {
                Console.WriteLine("未实现 type " + dg.type);
            }
            Console.WriteLine("共改: " + files.Length);
        }
        class dginfo
        {
            //1 加前缀
            //2 修改后缀名
            //3 改所有文件名为3位顺序号
            //4 所有文件的名加后缀
            //5 替换文件名
            //6 改前缀
            public int type { get; set; }
            public string item1 { get; set; }
            public string item2 { get; set; }
            public string item3 { get; set; }
            public string item4 { get; set; }
            public string filter1 { get; set; }
        }
    }
}
